<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>exchange</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Chapter 1. Boost.Core">
<link rel="up" href="../index.html" title="Chapter 1. Boost.Core">
<link rel="prev" href="enable_if.html" title="enable_if">
<link rel="next" href="explicit_operator_bool.html" title="explicit_operator_bool">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="enable_if.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="explicit_operator_bool.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="core.exchange"></a><a class="link" href="exchange.html" title="exchange">exchange</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="exchange.html#core.exchange.overview">Overview</a></span></dt>
<dt><span class="section"><a href="exchange.html#core.exchange.examples">Examples</a></span></dt>
<dt><span class="section"><a href="exchange.html#core.exchange.reference">Reference</a></span></dt>
</dl></div>
<div class="simplesect">
<div class="titlepage"><div><div><h3 class="title">
<a name="idm46136448944800"></a>Authors</h3></div></div></div>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
          Glen Fernandes
        </li></ul></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.exchange.overview"></a><a class="link" href="exchange.html#core.exchange.overview" title="Overview">Overview</a>
</h3></div></div></div>
<p>
        The header &lt;boost/core/exchange.hpp&gt; provides the function template
        <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">exchange</span></code> which is an implementation of
        the <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">exchange</span></code> function introduced in C++14.
        <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">exchange</span><span class="special">(</span><span class="identifier">o</span><span class="special">,</span> <span class="identifier">v</span><span class="special">)</span></code> replaces the value of <code class="computeroutput"><span class="identifier">o</span></code>
        with <code class="computeroutput"><span class="identifier">v</span></code> and returns the old
        value of <code class="computeroutput"><span class="identifier">o</span></code>.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.exchange.examples"></a><a class="link" href="exchange.html#core.exchange.examples" title="Examples">Examples</a>
</h3></div></div></div>
<p>
        The following example shows <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">exchange</span></code>
        used to simplify the implementation of a move constructor.
      </p>
<pre class="programlisting"><span class="identifier">Node</span><span class="special">(</span><span class="identifier">Node</span><span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span>
  <span class="special">:</span> <span class="identifier">head_</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">exchange</span><span class="special">(</span><span class="identifier">other</span><span class="special">.</span><span class="identifier">head_</span><span class="special">,</span> <span class="keyword">nullptr</span><span class="special">))</span>
  <span class="special">,</span> <span class="identifier">tail_</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">exchange</span><span class="special">(</span><span class="identifier">other</span><span class="special">.</span><span class="identifier">tail_</span><span class="special">,</span> <span class="keyword">nullptr</span><span class="special">))</span> <span class="special">{</span> <span class="special">}</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.exchange.reference"></a><a class="link" href="exchange.html#core.exchange.reference" title="Reference">Reference</a>
</h3></div></div></div>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
  <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">U</span> <span class="special">=</span> <span class="identifier">T</span><span class="special">&gt;</span>
  <span class="keyword">constexpr</span> <span class="identifier">T</span> <span class="identifier">exchange</span><span class="special">(</span><span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">t</span><span class="special">,</span> <span class="identifier">U</span><span class="special">&amp;&amp;</span> <span class="identifier">u</span><span class="special">);</span>
<span class="special">}</span>
</pre>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="core.exchange.reference.functions"></a><a class="link" href="exchange.html#core.exchange.reference.functions" title="Functions">Functions</a>
</h4></div></div></div>
<p>
          <span class="bold"><strong><code class="computeroutput"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span>
          <span class="identifier">U</span> <span class="special">=</span>
          <span class="identifier">T</span><span class="special">&gt;</span>
          <span class="keyword">constexpr</span> <span class="identifier">T</span>
          <span class="identifier">exchange</span><span class="special">(</span><span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">t</span><span class="special">,</span> <span class="identifier">U</span><span class="special">&amp;&amp;</span> <span class="identifier">u</span><span class="special">);</span></code></strong></span>
        </p>
<p>
          Equivalent to:
        </p>
<pre class="programlisting"><span class="identifier">T</span> <span class="identifier">v</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">t</span><span class="special">);</span>
<span class="identifier">t</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span><span class="identifier">U</span><span class="special">&gt;(</span><span class="identifier">u</span><span class="special">);</span>
<span class="keyword">return</span> <span class="identifier">v</span><span class="special">;</span>
</pre>
</div>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2014 Peter Dimov<br>Copyright © 2014 Glen Fernandes<br>Copyright © 2014 Andrey Semashev<p>
        Distributed under the <a href="http://boost.org/LICENSE_1_0.txt" target="_top">Boost
        Software License, Version 1.0</a>.
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="enable_if.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="explicit_operator_bool.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
